package com.triangulator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.util.AttributeSet;
import android.util.Log;


public class TopLevelView extends View {

	private static final String TAG = "TopLevelView";
	
	private static final int X_MIN_DISTANCE = 170;
	private static final int Y_MAX_DISTANCE = 150;
	private static final int MAX_TIME_DELAY = 700;
	
	private float old_x;
	private float old_y;
	
	private Paint paint = new Paint();
	
	private long time;
	private boolean tap_up;
	private boolean disableMyMaps;
	private boolean drawARect;

	private float rect_left;
	private float rect_right;
	private float rect_top;
	private float rect_bottom;
	
	public static float center_width;
	public static float center_height;
	
	int val;
	
	private boolean firstRun;
	private boolean centerRect;
	private boolean blank;

	public TopLevelView(Context context, AttributeSet attrs) {
		super(context, attrs);

		paint.setAntiAlias(true);
		
		old_x=0;
		old_y=0;
		
		time=0;
		
		tap_up=false;
		
		drawARect = false;
		
		center_width = center_height = -1;
		
		val=0;
		
		firstRun = true;
		blank = false;
		centerRect = false;
	}

	@Override
	public void onDraw(Canvas canvas) {
		
		int len=20;
		
		// only here you can initialize them using the getTitleAndStatusHeight() function
		// otherwise they get initialized with a wrong value
		if(firstRun)
		{
			firstRun = false;
			
			center_width = Triangulator.width/2;
			// tested for the square box and we get exactly the picture of what is inside the box
			center_height = Triangulator.height/2 - ((Triangulator) getContext()).getTitleAndStatusHeight()/2;
		}
		
		if(!blank)
		{
			if(drawARect) // rect ofr the various redientificator modes
			{
				Log.w(TAG, "drawing RECT: " + (val++));
				
				
				if(centerRect)
				{
					// calculate and draw rectangle
					float newH = Triangulator.height * Reidentificator.getHeightRatio();
					float newW = Triangulator.width  * Reidentificator.getWidthRatio();
					
					rect_left = center_width - newW/2;
					rect_right = center_width + newW/2;
					
					rect_top =  center_height - newH/2;
					rect_bottom = center_height + newH/2;
					
					
					paint.setColor(Color.BLACK);
					paint.setStyle(Paint.Style.FILL);
					paint.setAlpha(180);
					
					canvas.drawRect(0, 0, Triangulator.width, rect_top, paint);
					canvas.drawRect(0, rect_bottom, Triangulator.width, Triangulator.height, paint);
					canvas.drawRect(0, rect_top, rect_left, rect_bottom, paint);
					canvas.drawRect(rect_right, rect_top, Triangulator.width, rect_bottom, paint);
				}
				
				paint.setColor(Color.BLUE);
				paint.setStyle(Paint.Style.STROKE);
				paint.setStrokeWidth(3);
				paint.setAlpha(150);
				
				canvas.drawRect(rect_left, rect_top, rect_right, rect_bottom, paint);
			}
			else // circle for the Triangulator mode
			{
				paint.setColor(Color.BLUE);
				paint.setStyle(Paint.Style.STROKE);
				paint.setStrokeWidth(3);
				paint.setAlpha(150);
				
				canvas.drawCircle(center_width, center_height, 50, paint);
				canvas.drawLine(center_width-len/2, center_height, center_width+len/2, center_height, paint);
				canvas.drawLine(center_width, center_height-len/2, center_width, center_height+len/2, paint);
			}
		}
	}

	public void drawRectCenter()
	{
		drawARect = true;
		centerRect = true;
		blank = false;
		
		invalidate();
	}
	
	public void blank(boolean trueOrFalse)
	{
		blank = trueOrFalse;
		
		invalidate();
	}
	
	public void disableMaps(boolean trueOrFalse)
	{
		disableMyMaps = trueOrFalse;
	}
	
	public void drawRect(float left, float top, float width_m, float height_m)
	{
		drawARect = true;
		blank = false;
		centerRect = false;
		
		Log.i(TAG, "left "+ left + " top " + top + " width: " + width_m + " height: " + height_m);
		
		Log.i(TAG, "Triangulator.width: "+Triangulator.width +" Reidentificator.mScaleWidth: " + Reidentificator.mScaleWidth + " height: " + (Triangulator.height - ((Triangulator) getContext()).getTitleAndStatusHeight()));
		
		rect_left = left * Triangulator.width / Reidentificator.mScaleWidth;
		rect_top =  top * (Triangulator.height - ((Triangulator) getContext()).getTitleAndStatusHeight()) / Reidentificator.mScaleHeight;
		rect_right = rect_left +  Triangulator.width  * Reidentificator.getWidthRatio();
		rect_bottom = rect_top + (Triangulator.height - ((Triangulator) getContext()).getTitleAndStatusHeight()) * Reidentificator.getHeightRatio();
		
		Log.i(TAG, "x "+ rect_left + " y: "+ rect_top + " x2 "+rect_right + " y2: " + rect_bottom + " width: " + Triangulator.width  * Reidentificator.getWidthRatio() + " height: " + (Triangulator.height - ((Triangulator) getContext()).getTitleAndStatusHeight()) * Reidentificator.getHeightRatio());
		
		invalidate();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent me)
	{
		if(disableMyMaps)
			return true;
		
		long now = System.currentTimeMillis();
		
		if(me.getAction() == MotionEvent.ACTION_DOWN)
		{
			if (tap_up==true && (now - time) < 500 && (now - time) > 100)
			{
				Log.d(TAG, "-" + "doubleTap" + "-");
				//((Triangulator) getContext()).startMaps(true);
				return false;
			}
			else	// too slow
			{
				Log.v(TAG, "-" + "doubleTap TOO SLOW!" + "-");
				tap_up=false;
			}
			
			Log.v(TAG, "down: "+me.getX()+" "+me.getY());
			
			old_x=me.getX();
			old_y=me.getY();
			
			time=now;
		}
		else if(me.getAction() == MotionEvent.ACTION_UP)
		{
			Log.v(TAG, "up "+me.getX()+" "+me.getY());
			
			if(now-time < MAX_TIME_DELAY && 
					Math.abs(me.getX()-old_x) > X_MIN_DISTANCE && 
					Math.abs(me.getY()-old_y) < Y_MAX_DISTANCE)
			{
				Log.d(TAG, "slide");
				
				if(me.getX()-old_x > 0)
					((Triangulator) getContext()).startMaps(false);
				else
					((Triangulator) getContext()).startMaps(true);
				
				return false;
			}
			else
				tap_up=true;
		}
		
		return true;
	}
}